﻿@model MAPDavy.Entities.Term

@{
    ViewBag.Title = "ConfigTerm";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section JavaScript{
    <script type="text/javascript">
        $(document).ready(function () {
            $('#pnlSubject').hide('slow');
            $('#pnlTest').hide('slow');
            $("#listaTest").css("height", "300px");
            $("#listaTestGrade").css("height", "300px");

            $("#btnAdd").button({
                icons: {
                    secondary: "ui-icon-seek-next"
                }
            })

            $("#btnRemove").button({
                icons: {
                    primary: "ui-icon-seek-prev"
                }
            })

            $("#btnConfigGrade").click(function () {
                $('#pnlSubject').show('slow');
                $('#Grade').attr('disabled', 'true');
            });

            $("#btnSelectGrade").click(function () {
                $('#pnlSubject').hide('slow');
                $('#pnlTest').hide('slow');
                $('#Grade').removeAttr('disabled');
            });

            $("#btnLoadTest").click(function () {
                $('#pnlTest').show('slow');

                var _termId = $("#Id").val();
                var _gradeId = $("#Grade option:selected").val();
                var subject = $('#Subject option:selected').val();

                $.getJSON(
                    '@Url.Action("GetTestServiceJson", "Test")',
                    { subjectId: subject },
                    function (data) {
                        $("#listaTest").html('');
                        $.each(data, function (i, item) {
                            $("#listaTest").append(
                                $('<option>').text(item.Test).val(item.Id)
                            ).find('option:last').hide().fadeIn("slow");
                        });
                    });

                $.getJSON(
                    '@Url.Action("GetTestbyTermServiceJson", "Test")',
                    { termId: _termId, gradeId: _gradeId, subjectId: subject },
                    function (data) {
                        $("#listaTestGrade").html('');
                        $.each(data, function (i, item) {
                            $("#listaTestGrade").append(
                                $('<option>').text(item.Test).val(item.Id)
                            ).find('option:last').hide().fadeIn("slow");
                        });
                    });
            });

            $("#btnRemove").click(function () {
                var select = $("#listaTestGrade option:selected").length;

                var _termId = $("#Id").val();
                var _gradeId = $("#Grade option:selected").val();

                if (select != 0) {
                    jConfirm('Desea Quitar el Item(s) Seleccionado(s)', 'Confirmación', function (r) {
                        if (r) {
                            $("#listaTestGrade option:selected").each(function () {

                                var _testId = $(this).val();
                                var _test = $(this).text();
                                var option = $(this);

                                $.post(
                                    '@Url.Action("RemoveTestGrade", "Term")',
                                    { termId: _termId, gradeId: _gradeId, testId: _testId },
                                    function (data) {
                                        if (data.rtp) {
                                            $(option).fadeOut("slow", function () {
                                                $(option).remove();
                                            });
                                        }
                                        else {
                                            jAlert("No se puede(n) quitar el Item(s) seleccionado(s) porque tiene información Asociada", "Advertencia");
                                        }
                                    },
                                    "json"
                                );

                            });
                        }
                    });
                }
            });

            $("#btnAdd").click(function () {

                var _termId = $("#Id").val();
                var _anio = $("#Anio").val();
                var _gradeId = $("#Grade option:selected").val();

                $("#listaTest option:selected").each(function () {

                    var testId = $(this).val();
                    var test = $(this).text();
                    var count = $('#listaTestGrade option').length;

                    var _testId = testId;

                    if (count == 0) {

                        $.post(
                                '@Url.Action("AsignarTest", "Term")',
                                { termId: _termId, gradeId: _gradeId, testId: _testId, anio: _anio },
                                function (ok) {
                                    if (ok) {
                                        $("#listaTestGrade").append(
                                            $('<option>').text(test).val(testId)
                                        ).find('option:last').hide().fadeIn("slow");
                                    }
                                    else {
                                        jAlert("No se ha podito agregar el Test:<br/><b>" + test + "</b>", "Advertencia");
                                    }
                                },
                                "json"
                           );
                    }
                    else {
                        jAlert("Solo  puedes puedes asignar un Test", "Advertencia");
                        /*var existe = false;
                        var msg = "";

                        $("#listaTestGrade option").each(function () {
                            if (testId == $(this).val()) {
                                existe = true;
                                msg = msg + $(this).text() + "<br/>";
                                //jAlert("El Test:<br/><b>" + msg + "</b>Ya se encuetra Asignado", "Mensaje");
                            }
                        });

                        if (!existe) {

                            $.post(
                                '@Url.Action("AsignarTest", "Term")',
                                { termId: _termId, gradeId: _gradeId, testId: _testId, anio: _anio },
                                function (ok) {
                                    if (ok) {
                                        $("#listaTestGrade").append(
                                            $('<option>').text(test).val(testId)
                                        ).find('option:last').hide().fadeIn("slow");
                                    }
                                    else {
                                        jAlert("No se ha podito agregar el Test:<br/><b>" + test + "</b>", "Advertencia");
                                    }
                                },
                                "json"
                           );
                        }*/
                    }
                });
            });
        });
    </script>
}    
<div class="cpanel ui-widget ui-widget-content ui-corner-all">
    <table class="toolbar">
        <tr>
            <td valign="middle" >
                <img src="@Url.Content("~/Content/images/assignTask.png")" alt="evaluaciones" />
            </td>
            <td class="tdHeader"><span class="header">Configurar Evaluación: @Html.DisplayFor(model => model.NameTerm)</span></td>
            <td><center>
                <a href="@Url.Action("Index", "Term")">
                    <img src="@Url.Content("~/Content/images/back.png")" title="Candelar" alt="Candelar" />
                    <br /><span class="label">Cancelar</span>
                </a></center>
            </td>
        </tr>
    </table>
</div>        
<div class = "titleView ui-widget ui-widget-content ui-corner-all cerrado">
    @Html.HiddenFor(model => model.Id) 
    @Html.HiddenFor(model => model.Anio)  
        Grade: @Html.DropDownList("Grade") 
        <button id="btnConfigGrade">Configurar Grado</button>
        <button id="btnSelectGrade">Seleccionar otro Grado</button>       
</div>
<div id="pnlSubject" class = "titleView ui-widget ui-widget-content ui-corner-all">
    <div class="display-label">
        Grade: @Html.DropDownList("Subject") 
        <button id="btnLoadTest">Cargar Test's</button> 
    </div>       
</div>
<div id="pnlTest" class = "titleView ui-widget ui-widget-content ui-corner-all">
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <center><b>Test's Disponibles</b></center>
                <select name="listaTest[]"  multiple="multiple" id="listaTest" >
                </select>
            </td>
            <td valign="middle">
                <center>
                    <button id="btnAdd">Agregar</button><br /><br />
                    <button id="btnRemove">Quitar</button>
                </center>                
            </td>
            <td>
                <center><b>Test's Asignados</b></center>
                <select name="listaTestGrade[]"  multiple="multiple" id="listaTestGrade" >
                </select>
            </td>
        </tr>
    </table>     
</div>